#### Figure S7: Norm Violations Support (Continuous Histogram) ####

# Libraries
# library(here)
# library(rio)
# library(tidyverse)
# library(ggthemes)
# library(srvyr)

# data_pnas = import(here("Data","data_pnas.rds"))
# source(here("Code","Functions","funcs_theme.R"))

norm_ind_c = paste0("norm_",c("judges","polling","executive","censorship","loyalty"),"c")

cs_summary_c = data_pnas |> 
  filter(pid %in% c("Democrat","Republican")) |> # Just D/R
  select(pid, all_of(c(norm_ind_c)),
         uid, weight) |> 
  pivot_longer(-c("pid","uid","weight"), names_to = "var", values_to = "val") |> 
  mutate(var_typ = ifelse(str_detect(var,"norm"), "Democratic Norms", "Violence"),
         var = recode_factor(var,
                             norm_pollingc = "Reduce outparty polling stations",
                             norm_loyaltyc = "More loyal to party than Constitution\nin contested elections",
                             norm_judgesc = "Ignore outparty court decisions",
                             norm_executivec = "President should circumvent Congress",
                             norm_censorshipc = "Censor partisan media")
  ) |> 
  filter(var != "President should circumvent Congress")

fig_s7 = cs_summary_c |> 
  ggplot(aes(x = val, 
             group = pid,
             fill = pid)) +
  geom_histogram(aes(y = ..density.., weight = weight),
                 position = "identity", binwidth = 1, alpha =.5, color = "black") +
  facet_wrap(~ var, ncol=1) +
  scale_fill_manual(name = "Party",
                    values = c("blue","red")) +
  scale_x_continuous(labels = c("Strongly\ndisagree","Disagree","Neither agree\nnor disagree",
                                "Agree","Strongly\nagree"),
                     breaks = 1:5) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = NULL,
       y = NULL) +
  theme_prl()

print(fig_s7)

ggsave(here("Plots","Supplementary","figure_s7.png"),
       fig_s7,
       units = "in",
       width = 5, height = 7,
       dpi = 600)